set optimize_read_in_order=1;
set enable_analyzer=1;

drop table if exists test;

{% for create_command in ['create table test (id UInt64, a Tuple(b Tuple(c UInt32, d String, e Array(UInt32))), data String) engine=MergeTree order by (a.b.c, a.b.d) settings min_rows_for_wide_part=100000000, min_bytes_for_wide_part=1000000000, index_granularity = 1;',
                          'create table test (id UInt64, a Tuple(b Tuple(c UInt32, d String, e Array(UInt32))), data String) engine=MergeTree order by (a.b.c, a.b.d) settings min_rows_for_wide_part=1, min_bytes_for_wide_part=1, vertical_merge_algorithm_min_columns_to_activate=10, vertical_merge_algorithm_min_rows_to_activate=10000000000, index_granularity = 1;',
                          'create table test (id UInt64, a Tuple(b Tuple(c UInt32, d String, e Array(UInt32))), data String) engine=MergeTree order by (a.b.c, a.b.d) settings min_rows_for_wide_part=1, min_bytes_for_wide_part=1, vertical_merge_algorithm_min_columns_to_activate=1, vertical_merge_algorithm_min_rows_to_activate=1, index_granularity = 1;'] -%}

select '{{ create_command }}';

{{ create_command }}

system stop merges test;
insert into test select number, tuple(tuple(number % 2, 'str_' || number % 3, range(number))), 'Hello, World!' from numbers(4);
insert into test select number, tuple(tuple(number % 2 + 4, 'str_' || number % 3 + 4, range(number))), 'Hello, World!' from numbers(4, 4);

select * from test order by a.b.c, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.c);
select * from test order by a.b.c desc, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.c desc);
select * from test order by a.b.c, a.b.d, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.c, a.b.d);
select * from test order by a.b.c desc, a.b.d, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.c desc, a.b.d);
select * from test order by a.b.c, a.b.d desc, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.c, a.b.d desc);
select * from test order by a.b.c desc, a.b.d desc, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.c desc, a.b.d desc);

select * from test where a.b.c = 0 order by id;
select trimLeft(explain) from (explain indexes=1 select * from test where a.b.c = 0);
select * from test where a.b.c = 0 and a.b.d = 'str_0' order by id;
select trimLeft(explain) from (explain indexes=1 select * from test where a.b.c = 0 and a.b.d = 'str_0');

select count() from test where a.b.c = 0 settings optimize_use_implicit_projections = 1;
select trimLeft(explain) from (explain indexes=1 select count() from test where a.b.c = 0 settings optimize_use_implicit_projections = 1);
select count() from test where a.b.c = 0 and a.b.d = 'str_0' settings optimize_use_implicit_projections = 1;
select trimLeft(explain) from (explain indexes=1 select count() from test where a.b.c = 0 and a.b.d = 'str_0' settings optimize_use_implicit_projections = 1);

system start merges test;
optimize table test final;
select count() from system.parts where active and table = 'test' and database = currentDatabase();

select * from test order by a.b.c, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.c);
select * from test order by a.b.c desc, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.c desc);
select * from test order by a.b.d, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.d);
select * from test order by a.b.d desc, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.d desc);
select * from test order by a.b.c, a.b.d, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.c, a.b.d);
select * from test order by a.b.c desc, a.b.d, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.c desc, a.b.d);
select * from test order by a.b.c, a.b.d desc, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.c, a.b.d desc);
select * from test order by a.b.c desc, a.b.d desc, id;
select trimLeft(explain) from (explain sorting=1 select * from test order by a.b.c desc, a.b.d desc);

select * from test where a.b.c = 0 order by id;
select trimLeft(explain) from (explain indexes=1 select * from test where a.b.c = 0);
select * from test where a.b.c = 0 and a.b.d = 'str_0' order by id;
select trimLeft(explain) from (explain indexes=1 select * from test where a.b.c = 0 and a.b.d = 'str_0');

select count() from test where a.b.c = 0 settings optimize_use_implicit_projections = 1;
select trimLeft(explain) from (explain indexes=1 select count() from test where a.b.c = 0 settings optimize_use_implicit_projections = 1);
select count() from test where a.b.c = 0 and a.b.d = 'str_0' settings optimize_use_implicit_projections = 1;
select trimLeft(explain) from (explain indexes=1 select count() from test where a.b.c = 0 and a.b.d = 'str_0' settings optimize_use_implicit_projections = 1);

drop table test;

{% endfor -%}

